﻿# 设置输出文件名
$outputFile = "sword_result.txt"

# 清空输出文件（如果存在）
If (Test-Path $outputFile) {
    Remove-Item $outputFile
}

# 添加系统信息
"System Information:" >> $outputFile 
systeminfo | Out-File -FilePath $outputFile -Append 

# 添加服务器互联网出口 IP 信息
"Internet Export IP Information:" >> $outputFile 
curl myip.ipip.net | Out-File -FilePath $outputFile -Append 

# 添加防火墙状态信息
"Firewall Status Information:" >> $outputFile
Get-NetFirewallProfile | Format-Table Name, Enabled -AutoSize | Out-String | Out-File -FilePath $outputFile -Append 

# 添加内存使用信息
"Memory Usage Information:" >> $outputFile
Get-WmiObject -Class Win32_OperatingSystem | Select-Object FreePhysicalMemory, TotalVisibleMemorySize | Out-File -FilePath $outputFile -Append

# 添加 CPU 使用信息
"CPU Usage Information:" >> $outputFile
Get-WmiObject -Class Win32_Processor | Measure-Object -Property LoadPercentage -Average | Format-List -Property Average | Out-File -FilePath $outputFile -Append

# 添加 hosts 文件信息
"Hosts File Information:" >> $outputFile
Get-Content -Path 'C:\Windows\System32\drivers\etc\hosts' | Out-File -FilePath $outputFile -Append

# 添加本地用户信息
"Local User Information:" >> $outputFile
Get-WmiObject -Class Win32_UserAccount | Format-Table Name, LocalAccount -AutoSize | Out-String | Out-File -FilePath $outputFile -Append

# 添加基于 CPU 占用大小的进程列表信息
"Process List Based on CPU Usage:" >> $outputFile
Get-Process | Sort-Object -Property CPU -Descending | Format-Table -AutoSize | Out-String | Out-File -FilePath $outputFile -Append

# 添加 ARP 表信息
"ARP Table Information:" >> $outputFile
Get-WmiObject -Class Win32_NetworkAdapterConfiguration | Where-Object { $_.IPEnabled } | Format-Table IPAddress, DefaultIPGateway, DHCPLeaseExpires -AutoSize | Out-String | Out-File -FilePath $outputFile -Append

# 添加网络路由信息
"Network Routing Information:" >> $outputFile
route print | Out-File -FilePath $outputFile -Append

# 添加网络 DNS 信息
"Network DNS Information:" >> $outputFile
ipconfig /all | Select-String -Pattern 'DNS' | Out-File -FilePath $outputFile -Append

# 添加 TCP 网络连接信息
"TCP Network Connections:" >> $outputFile
netstat -ano | Select-String -Pattern 'TCP' | Out-File -FilePath $outputFile -Append

# 添加 UDP 网络连接信息
"UDP Network Connections:" >> $outputFile
netstat -ano | Select-String -Pattern 'UDP' | Out-File -FilePath $outputFile -Append

# 添加防火墙策略信息
"Firewall Policy Information:" >> $outputFile
Get-NetFirewallRule | Format-Table Name, Action, Enabled -AutoSize | Out-String | Out-File -FilePath $outputFile -Append

# 添加系统自启动项信息
"System Startup Items:" >> $outputFile
(Get-ItemProperty HKCU:\Software\Microsoft\Windows\CurrentVersion\Run) | Format-Table Path -AutoSize | Out-String | Out-File -FilePath $outputFile -Append
(Get-ItemProperty HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Run) | Format-Table Path -AutoSize | Out-String | Out-File -FilePath $outputFile -Append

# 添加系统计划任务信息
"Scheduled Tasks:" >> $outputFile
Get-ScheduledTask | Format-Table Name, LastRunTime, NextRunTime -AutoSize | Out-String | Out-File -FilePath $outputFile -Append

# 添加系统服务信息
"System Services:" >> $outputFile
Get-Service | Format-Table Name, Status, StartType -AutoSize | Out-String | Out-File -FilePath $outputFile -Append

# 添加 PowerShell 历史命令信息
"PowerShell History:" >> $outputFile
Get-History | Format-Table Command -AutoSize | Out-String | Out-File -FilePath $outputFile -Append

# 添加当前用户登录信息
"Current User Login Information:" >> $outputFile
whoami | Out-File -FilePath $outputFile -Append

# 添加新增用户日志信息
"New User Log Information:" >> $outputFile
Get-WinEvent -FilterHashtable @{LogName='Security'; ID=4720} | Format-Table TimeCreated, ProviderName, Id, Message -AutoSize | Out-String | Out-File -FilePath $outputFile -Append

# 添加软件安装信息
"Software Installation Information:" >> $outputFile
Get-WmiObject -Class Win32_Product | Select-Object Name, Version, InstallDate | Out-File -FilePath $outputFile -Append

# 添加共享文件夹信息
"Shared Folders Information:" >> $outputFile
Get-SmbShare | Format-Table Name, Path -AutoSize | Out-String | Out-File -FilePath $outputFile -Append

# 完成
Write-Output "All information has been saved to $outputFile."